% File: sc_fde_postprocess.m
% --------------------------
% This script implements output analysis, visualization, and restoration

% -------------------------------------------------------------------------
% test point - mse, var statistics
% -------------------------------------------------------------------------
MSE_BUF  = mse_buffer/numTrials;
MSE_TBUF = mse_test_buffer/numTrials;
VAR_BUF  = var_buffer/numTrials;
VAR_TBUF = var_test_buffer/numTrials;
SNR_BUF  = 1./MSE_BUF;
SNR_TBUF = 1./MSE_TBUF;
% -------------------------------------------------------------------------

SER_per = numerr/(nsymb*numTrials); % SER per subframe
SER     = squeeze(mean(SER_per,2)); % average SER
BER_per = bitnumerr/((nbit-decdel)*numTrials); % BER per subframe
BER     = squeeze(mean(BER_per,2));            % average BER
ser_mfb = ser_mfb/numTrials;
ber_mfb = ber_mfb/numTrials;
% Plot SER results.
figure();
semilogy(Eb_N0,SER,'-*',Eb_N0,ser_mfb,'r-');
xlabel('Eb/N_0 (dB)'); ylabel('SER');
ylim([5e-6 1]);
grid on

switch rx_profile
    case 1
        legend('IB-DFE armVar','MFB');
        title('QPSK-SC-FDE over Rayleigh Fading Channel');
        filename = 'sc_fde_ib_armVar.mat';
        save(filename, 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'path_delays_norm', 'doppler_max', 'pdp');
    case 2
        legend('cp-recon','MFB');
        title('QPSK-SC-FDE over Rayleigh Fading Channel');
        filename = 'sc_fde_cp_recon.mat';
        save(filename, 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'path_delays_norm', 'doppler_max', 'pdp');
    case 3
        legend('IB-DFE maxVar','MFB');
        title('QPSK-SC-FDE over Rayleigh Fading Channel');
        filename = 'sc_fde_ib_maxVar.mat';
        save(filename, 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'path_delays_norm', 'doppler_max', 'pdp');
    case 4
        legend('IB-DFE geomVar','MFB');
        title('QPSK-SC-FDE over Rayleigh Fading Channel');
        filename = 'sc_fde_ib_geomVar.mat';
        save(filename, 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'path_delays_norm', 'doppler_max', 'pdp');
    case 5
        legend('IB-DFE harmVar','MFB');
        title('QPSK-SC-FDE over Rayleigh Fading Channel');
        filename = 'sc_fde_ib_harmVar.mat';
        save(filename, 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'path_delays_norm', 'doppler_max', 'pdp');
    case 6
        legend('simp.','MFB');
        title_string  = sprintf('%dQAM-SC-FDE over %s Fading Channel', M_mod, chan_type);
        title(title_string);
        if (intlv_profile)
            filename = sprintf('testbench_sc_fde_ib_qam_simp_fip_%dits_%s_%d_%dqam_eb_n0_%d_%d_%dtrials.mat', N_IB, chan_type, nsymb, M_mod, min(Eb_N0), max(Eb_N0), numTrials);
        else
            filename = sprintf('testbench_sc_fde_ib_qam_simp_nofip_%dits_%s_%d_%dqam_eb_n0_%d_%d_%dtrials.mat', N_IB, chan_type, nsymb, M_mod, min(Eb_N0), max(Eb_N0), numTrials);
        end
        save(filename, 'N_IB', 'M_mod', 'chan_type', 'nsymb', 'numTrials', 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'MSE_BUF', 'MSE_TBUF', 'SNR_BUF', 'SNR_TBUF', 'VAR_BUF', 'VAR_TBUF');
    case 7
        legend('evo.','MFB');
        title_string  = sprintf('%dQAM-SC-FDE over %s Fading Channel', M_mod, chan_type);
        title(title_string);
        if (intlv_profile)
            filename = sprintf('testbench_sc_fde_ib_qam_evo_fip_%dits_%s_%d_%dqam_eb_n0_%d_%d_%dtrials.mat', N_IB, chan_type, nsymb, M_mod, min(Eb_N0), max(Eb_N0), numTrials);
        else
            filename = sprintf('testbench_sc_fde_ib_qam_evo_nofip_%dits_%s_%d_%dqam_eb_n0_%d_%d_%dtrials.mat', N_IB, chan_type, nsymb, M_mod, min(Eb_N0), max(Eb_N0), numTrials);
        end
        save(filename, 'N_IB', 'M_mod', 'chan_type', 'nsymb', 'numTrials', 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'MSE_BUF', 'MSE_TBUF', 'SNR_BUF', 'SNR_TBUF', 'VAR_BUF', 'VAR_TBUF');
    case 8
        legend('genie','MFB');
        title_string  = sprintf('%dQAM-SC-FDE over %s Fading Channel', M_mod, chan_type);
        title(title_string);
        if (intlv_profile)
            filename = sprintf('testbench_sc_fde_ib_qam_genie_fip_%dits_%s_%d_%dqam_eb_n0_%d_%d_%dtrials.mat', N_IB, chan_type, nsymb, M_mod, min(Eb_N0), max(Eb_N0), numTrials);
        else
            filename = sprintf('testbench_sc_fde_ib_qam_genie_nofip_%dits_%s_%d_%dqam_eb_n0_%d_%d_%dtrials.mat', N_IB, chan_type, nsymb, M_mod, min(Eb_N0), max(Eb_N0), numTrials);
        end
        save(filename, 'N_IB', 'M_mod', 'chan_type', 'nsymb', 'numTrials', 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'MSE_BUF', 'MSE_TBUF', 'SNR_BUF', 'SNR_TBUF', 'VAR_BUF', 'VAR_TBUF');
    otherwise
        legend('','MFB');
        title('QPSK-SC-FDE over Rayleigh Fading Channel');
        filename = 'sc_fde_mfb.mat';
        save(filename, 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'path_delays_norm', 'doppler_max', 'pdp');
end

if (code_profile)
    % Plot BER results.
    figure();
    semilogy(Eb_N0,BER,'-*',Eb_N0,ber_mfb,'r-');
    xlabel('Eb/N_0 (dB)'); ylabel('BER');
    ylim([5e-6 1]);
    grid on
    switch rx_profile
        case 1
            legend('IB-DFE','MFB');
            title('QPSK-SC-FDE over Rayleigh Fading Channel');
            filename = 'sc_fde_ib.mat';
        case 2
            legend('cp-recon','MFB');
            title('QPSK-SC-FDE over Rayleigh Fading Channel');
            filename = 'sc_fde_cp_recon.mat';
        otherwise
            legend('','MFB');
            title('QPSK-SC-FDE over Rayleigh Fading Channel');
            filename = 'sc_fde_mfb.mat';
    end
    save(filename, 'Eb_N0', 'SNR', 'SER', 'SER_per', 'BER', 'BER_per', 'ser_mfb', 'ber_mfb', 'path_delays_norm', 'doppler_max', 'pdp');
end

% End of script